package cn.minsh.lims.eoms.modules.app.controller;

import cn.minsh.lims.eoms.common.utils.PageUtils;
import cn.minsh.lims.eoms.common.utils.R;
import cn.minsh.lims.eoms.modules.app.controller.req.MaintenancePlanListReq;
import cn.minsh.lims.eoms.modules.app.controller.req.MaintenancePlanReq;
import cn.minsh.lims.eoms.modules.app.controller.res.MaintenancePlanListRes;
import cn.minsh.lims.eoms.modules.app.entity.MaintenancePlanEntity;
import cn.minsh.lims.eoms.modules.app.enums.PlanStatusEnum;
import cn.minsh.lims.eoms.modules.app.service.MaintenancePlanService;
import cn.minsh.lims.eoms.common.utils.ParamUtils;
import cn.minsh.lims.eoms.modules.app.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * 保养计划
 *
 * @author lht
 * @email sunlightcs@gmail.com
 * @date 2021-04-29 18:54:57
 */
@RestController
@RequestMapping("sys/maintenancePlan")
@Api(tags = "保养计划（优化保养级别修改为保养类型，改为字典维护）")
public class MaintenancePlanController {
    @Autowired
    private MaintenancePlanService maintenancePlanService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("保养计划列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "ok",response = MaintenancePlanListRes.class)
    })
    public R list(@RequestBody MaintenancePlanListReq req, HttpServletRequest request){
        try {
            PageUtils page = maintenancePlanService.queryListPage(req, UserUtils.getUserId(request));
            return R.ok().put("data", page);
        }catch (Exception e){
            return R.error(e.getMessage()) ;
        }
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("保养计划详情")
    @ApiResponses({
            @ApiResponse(code = 200,message = "ok",response = MaintenancePlanListRes.class)
    })
    public R info(@PathVariable("id") Long id, HttpServletRequest request){
        try {
            MaintenancePlanListRes res = maintenancePlanService.queryPlanById(id, UserUtils.getUserId(request));
            return R.ok().put("data", res);
        }catch (Exception e){
            return R.error(e.getMessage()) ;
        }

    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加保养计划")
    public R save(@RequestBody @Valid MaintenancePlanReq req,
                  BindingResult bindingResult, HttpServletRequest request){
        try {
            ParamUtils.check(bindingResult);
            maintenancePlanService.savePlan(req, UserUtils.getUserId(request));
            return R.ok();
        }catch (Exception e){
            return R.error(e.getMessage()) ;
        }

    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("更新保养计划")
    public R update(@RequestBody @Valid MaintenancePlanReq req,BindingResult bindingResult, HttpServletRequest request){
        try {
            ParamUtils.check(bindingResult);
            if (req.getId()==null){
                return R.error("记录id不能我空") ;
            }
            maintenancePlanService.updatePlanById(req, UserUtils.getUserId(request));
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage()) ;
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public R delete(@RequestParam("id") Long id, HttpServletRequest request){
        //未开始的计划可以删除
        MaintenancePlanEntity planEntity = maintenancePlanService.getById(id) ;
        if (planEntity == null || planEntity.getId()==null){
            return R.error("记录不存在") ;
        }
        /*if (PlanStatusEnum.STARTED.getCode().equals(planEntity.getStatus())){
            return R.error("进行中的计划不能删除") ;
        }
        if (PlanStatusEnum.OVER.getCode().equals(planEntity.getStatus())){
            return R.error("已结束的计划不能删除") ;
        }*/
        if(!PlanStatusEnum.NOT_STARTED.getCode().equals(planEntity.getStatus())){
            return R.error("仅未开始状态计划可删除") ;
        }
        if (PlanStatusEnum.NOT_STARTED.getCode().equals(planEntity.getStatus())){
            planEntity.setIsDel(1);
            maintenancePlanService.updateById(planEntity) ;
        }
        return R.ok();
    }

}
